package com.k.demo.mapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import com.k.demo.entity.GoodOrderDetail;

@Mapper
public interface GoodOrderDetailMapper {

	//批量加入订单明细信息
	boolean insertForeach(List<GoodOrderDetail> goodOrderDetailList);
	
	//查找当前订单下的所有订单明细
	List<GoodOrderDetail> getDetails(Integer orderid);
	
	
	//查找当天订单卖出去的菜品数量
	@Select("select nvl(sum(food_amount),0) from good_order_detail where order_id in (select order_id from good_order where trunc(order_create_time)=trunc(to_date(#{date},'yyyy-mm-dd hh24:mi:ss')))")
	int selectAllFoodNumberByDate(String date);
	
	@ResultMap("FoodSaleNumberMap")
	@Select("select food_name,nvl(sum(food_amount),0) from good_order_detail where order_id in (select order_id from good_order where trunc(order_create_time)=to_date(#{date},'yyyy-mm-dd')) group by food_name ")
	List<GoodOrderDetail> getFoodSaleNumber(String date);
	
	@ResultMap("FoodSaleNumberMap")
	@Select("select food_name,nvl(sum(food_amount),0),nvl(sum(food_total),0)  from good_order_detail where order_id in (select order_id from good_order where order_create_time>=to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss') and order_create_time<=to_date(#{end_date},'yyyy-mm-dd hh24:mi:ss')+1) and is_delete=1 group by food_name ")
	List<GoodOrderDetail> selectFoodSaleNumberByTime(String start_date,String end_date);
	//查询今天卖出去的菜品数量
	@Select("select nvl(sum(food_amount),0) from good_order_detail where order_id in (select order_id from good_order where trunc(order_create_time)=trunc(sysdate))")
	int selectAllFoodNumberToday();
	
	//查询时间段内的卖出去的菜品数,这里根据订单id分组
	@Select("select nvl(sum(food_amount),0) from good_order_detail where order_id in (select order_id from good_order where trunc(order_create_time)>=to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss') and trunc(order_create_time)<=to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss')+1  and  is_delete=1) group by order_id")
	int selectAllFoodNumber(String start_date,String end_date);
	
	
	@ResultMap("FoodSaleNumberMap")
	@Select("select food_name,nvl(sum(food_amount),0) from good_order_detail where order_id in (select order_id from good_order where trunc(order_create_time)=trunc(sysdate)) group by food_name ")
	List<GoodOrderDetail> getFoodSaleNumberToday();
	
	//返回当前订单的菜品总数
	@Select("select sum(food_amount) from good_order_detail where order_id=#{order_id} and is_delete=1")
	int sumFoodAmount(int order_id);
	
	//返回当前订单前两个菜品的名字并且组合
	String createOrderName(int order_id);
	
	//返回订单第一个菜品的菜品图片
	String createOrderPic(int order_id);
	
	//返回当天的菜品销量，并且根据菜品种类进行分组
	@Select("		select nvl(sum(g.food_total),0) as price,f.food_kind_id as id,sum(g.food_amount) as amount,k.food_kind_name as name from good_order_detail g left join food f on g.food_id=f.food_id  left join food_kind k on k.food_kind_id=f.food_kind_id where g.order_id in (select order_id from good_order where order_create_time>=to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss') and order_create_time<=to_date(#{end_date},'yyyy-mm-dd hh24:mi:ss')+1) and g.is_delete=1 group by f.food_kind_id,food_kind_name\r\n" + 
			"")
	List<HashMap<String, Object>> getFoodSaleNumWithKind(String start_date,String end_date);
	
	
	@Select("select nvl(sum(food_amount),0) from good_order_detail where food_id = #{food_id} and order_id in (select order_id from good_order where order_create_time>=TRUNC(SYSDATE, 'MM') and good_order.order_create_time<=last_day(SYSDATE)) group by food_name ")
	int selectFoodMonthSaleNumber(int food_id);
}
